package com.telit.firecommand.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telit.common.core.utils.FeignUtils;
import com.telit.firecommand.dao.CadPlotRoomDao;
import com.telit.firecommand.dto.PlotRoomReturnDto;
import com.telit.firecommand.entity.CadPlotRoom;
import com.telit.firecommand.service.ICadPlotRoomService;
import com.telit.upms.feign.RemoteUpmsService;
import com.telit.upms.vo.MicroSysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 标绘房间 服务实现类
 * </p>
 *
 * @author yummy
 * @since 2021-07-31
 */
@Service
@Transactional
public class CadPlotRoomServiceImpl extends ServiceImpl<CadPlotRoomDao, CadPlotRoom> implements ICadPlotRoomService {

    @Autowired
    private RemoteUpmsService remoteUpmsService;

    @Override
    public int add(CadPlotRoom cadPlotRoom) {
        return baseMapper.insert(cadPlotRoom);
    }

    @Override
    public int delete(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<Integer> deleteBatch(Integer[] ids) {
        List<Integer> values = new ArrayList<Integer>();
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                values.add(delete(id));
            }
        }
        return values;
    }

    @Override
    public int updateData(CadPlotRoom cadPlotRoom) {
        return baseMapper.updateById(cadPlotRoom);
    }

    @Override
    public CadPlotRoom findById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Integer searchCountByFireAlarmId(Long fireAlarmId, String status) {
        //查询当前联动标绘人数
        Integer count = baseMapper.selectCount(
                new QueryWrapper<CadPlotRoom>().eq("fire_alarm_id", fireAlarmId.toString()).eq("status", status));
        return count;
    }

    @Override
    public PlotRoomReturnDto searchUserNameByFireAlarmId(String fireAlarmId) {
        //时间区间查询
        PlotRoomReturnDto plotRoomReturnDto = ObjectUtil.defaultIfNull(baseMapper.searchMinAndMaxTime(fireAlarmId),
                new PlotRoomReturnDto());
        //人员id列表
        List<Long> userIdList = baseMapper.selectUserNameByFireAlarmId(fireAlarmId);
        List<String> userNameList = new ArrayList<>();
        //2022-05-18 优化 避免过度请求 -------fixed by yzx-------start
        /*if(userIdList!=null && userIdList.size()>0){
            for (Long userId :
                    userIdList) {
                //通过用户ID获取到用户的姓名
                SysUserDetailVo sysUserVo = userInfoService.getUserInfo(userId);
                if(sysUserVo !=null && StringUtils.isNotBlank(sysUserVo.getRealName())) {
                    userNameList.add(sysUserVo.getRealName());
                }
            }
            //set到plotRoomReturnDto
            plotRoomReturnDto.setUserNameList(userNameList);
        }

        return plotRoomReturnDto;*/
        if (CollUtil.isEmpty(userIdList)) {
            plotRoomReturnDto.setUserNameList(userNameList);
            return plotRoomReturnDto;
        }

        //处理用户名称
        com.telit.common.core.utils.Result<Map<Long, MicroSysUserVo>> selectByBatchIdsResult = remoteUpmsService.selectByBatchIds(
                userIdList, true);
        Map<Long, MicroSysUserVo> microSysUserVoMap = FeignUtils.unwrap(selectByBatchIdsResult);
        Collection<MicroSysUserVo> microSysUserVos = microSysUserVoMap.values();
        if (CollectionUtil.isNotEmpty(microSysUserVos)) {
            userNameList.addAll(microSysUserVos.stream().map(MicroSysUserVo::getRealName).collect(Collectors.toList()));
        }
        plotRoomReturnDto.setFireAlarmId(fireAlarmId);
        return plotRoomReturnDto.setUserNameList(userNameList);
        //2022-05-18 优化 避免过度请求 -------fixed by yzx-------end
    }

    @Override
    public Boolean searchInRoomByFireAlarmIdAndUserId(String fireAlarmId, Long userId) {
        String status = "1";
        //用户ID和火情ID判断某用户是否在房间
        Integer count = baseMapper.searchInRoomByFireAlarmIdAndUserId(fireAlarmId, userId, status);
        return count > 0 ? true : false;
    }


    @Override
    public IPage<CadPlotRoom> findListByPage(CadPlotRoom cadPlotRoom, Integer page, Integer pageCount) {
        IPage<CadPlotRoom> cadPlotRoomPage = new Page<>(page, pageCount);
        QueryWrapper<CadPlotRoom> wrapper = new QueryWrapper<>();
        if (cadPlotRoom != null) {
            if (cadPlotRoom.getFireAlarmId() != null) {
                wrapper.eq("fire_alarm_id", cadPlotRoom.getFireAlarmId());
            }
            if (cadPlotRoom.getStatus() != null) {
                wrapper.eq("status", cadPlotRoom.getStatus());
            }
            if (cadPlotRoom.getUserId() != null) {
                wrapper.eq("user_id", cadPlotRoom.getUserId());
            }
            if (cadPlotRoom.getCreateTime() != null) {
                wrapper.eq("create_time", cadPlotRoom.getCreateTime());
            }
            if (cadPlotRoom.getUpdateTime() != null) {
                wrapper.eq("update_time", cadPlotRoom.getUpdateTime());
            }
        }
        return baseMapper.selectPage(cadPlotRoomPage, wrapper);
    }

    @Override
    public List<CadPlotRoom> searchNoPage(CadPlotRoom cadPlotRoom) {
        QueryWrapper<CadPlotRoom> wrapper = new QueryWrapper<>();
        if (cadPlotRoom != null) {
            if (cadPlotRoom.getFireAlarmId() != null) {
                wrapper.eq("fire_alarm_id", cadPlotRoom.getFireAlarmId());
            }
            if (cadPlotRoom.getStatus() != null) {
                wrapper.eq("status", cadPlotRoom.getStatus());
            }
            if (cadPlotRoom.getUserId() != null) {
                wrapper.eq("user_id", cadPlotRoom.getUserId());
            }
            if (cadPlotRoom.getCreateTime() != null) {
                wrapper.eq("create_time", cadPlotRoom.getCreateTime());
            }
            if (cadPlotRoom.getUpdateTime() != null) {
                wrapper.eq("update_time", cadPlotRoom.getUpdateTime());
            }
        }
        return baseMapper.selectList(wrapper);
    }

}
